<?php

namespace app\member\controller\sys_admin;

use app\AdminController;
use app\member\model\UsersModel;

use app\member\model\UsersLevelModel;
use app\member\model\UsersBindModel;
use app\distribution\model\DividendRoleModel;
use app\distribution\model\DividendModel;
use app\weixin\model\WeiXinUsersModel;

use think\Db;
use think\facade\Cache;

/**
 * 会员管理
 * Class Index
 * @package app\store\controller
 */
class Users extends AdminController
{
    public $is_ban = 0;
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new UsersModel();
    }
    /*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->assign("start_date", date('Y/m/01', strtotime("-1 months")));
        $this->assign("end_date", date('Y/m/d'));
        $this->getList(true);
        $this->assign("roleOpt", arrToSel($this->roleList, $this->search['roleId']));
        $this->assign("levelOpt", arrToSel($this->levelList, $this->search['levelId']));
        return $this->fetch('sys_admin/users/index');
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false, $is_ban = -1)
    {

        $this->search['roleId'] = input('rode_id', 0, 'intval');
        $this->search['levelId'] = input('level_id', 0, 'intval');
        $this->search['keyword'] = input("keyword");
        $this->search['time_type'] = input("time_type");

        $this->assign("is_ban", $this->is_ban);
        $this->assign("search", $this->search);
        $DividendRoleModel = new DividendRoleModel();
        $this->roleList = $DividendRoleModel->getRows();
        $this->assign("roleList", $this->roleList);
        $UsersLevelModel = new UsersLevelModel();
        $this->levelList = $UsersLevelModel->getRows();
        $this->assign("levelList", $this->levelList);
        $where[] = ' is_ban = ' . $this->is_ban;

        $reportrange = input('reportrange');
        if (empty($reportrange) == false) {
            $dtime = explode('-', $reportrange);
        }
        switch ($this->search['time_type']) {
            case 'reg_time':
                $where[] = ' u.reg_time between ' . strtotime($dtime[0]) . ' AND ' . (strtotime($dtime[1]) + 86399);
                break;
            case 'login_time':
                $where[] = ' u.login_time between ' . strtotime($dtime[0]) . ' AND ' . (strtotime($dtime[1]) + 86399);
                break;
            case 'buy_time':
                $where[] = ' u.last_buy_time between ' . strtotime($dtime[0]) . ' AND ' . (strtotime($dtime[1]) + 86399);
                break;
            default:
                break;
        }

        if ($this->search['roleId'] > 0) {
            $where[] = ' u.role_id = ' . $this->search['roleId'];
        }
        if ($this->search['levelId'] > 0) {
            $level = $this->levelList[$this->search['levelId']];
            $where[] = ' uc.total_integral between ' . $level['min'] . ' AND ' . $level['max'];
        }

        if (empty($this->search['keyword']) == false) {
            if (is_numeric($this->search['keyword'])) {
                $where[] = "  u.user_id = '" . ($this->search['keyword']) . "' or mobile like '" . $this->search['keyword'] . "%'";
            } else {
                $where[] = " ( u.user_name like '" . $this->search['keyword'] . "%' or u.nick_name like '" . $this->search['keyword'] . "%' )";
            }
        }

        $sort_by = input("sort_by", 'DESC', 'trim');
        $order_by = 'u.user_id';
        $is_export =  input('is_export',0,'intval');
        if ($is_export > 0) {
            return $this->export($where);
        }
        $viewObj = $this->Model->alias('u')->join("users_account uc", 'u.user_id=uc.user_id', 'left')->where(join(' AND ', $where))->field('u.*,uc.balance_money,uc.total_dividend,uc.use_integral,uc.bean_value,uc.d_use_integral')->order($order_by . ' ' . $sort_by);

        $data = $this->getPageList($this->Model, $viewObj);
        $data['order_by'] = $order_by;
        $data['sort_by'] = $sort_by;

        foreach($data as $k =>$v){
            foreach($v as $k1 =>$v1){
                if(!empty($v1['proxy'])){
                    $level_type = Db::name('main_region')->where('id',$v1['proxy'])->find();
                    if($level_type['level_type'] == 1){
                        $v1['city_name'] = '省代';
                        $v1['region_name'] = Db::name('main_region')->where('id',$level_type['id'])->value('name');
                    }elseif($level_type['level_type'] == 2){
                        $v1['city_name'] = '市代';
                        $v1['region_name'] = Db::name('main_region')->where('id',$level_type['id'])->value('merger_name');
                    }elseif($level_type['level_type'] == 3){
                        $v1['city_name'] = '区代';
                        $v1['region_name'] = Db::name('main_region')->where('id',$level_type['id'])->value('merger_name');
                    }
                    $data[$k][$k1] = $v1;
                }
            }
        }
//        dump($data['list']);exit;
        $this->assign("data", $data);
        if ($runData == false) {
            $data['content'] = $this->fetch('sys_admin/users/list');
            return $this->success('', '', $data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 重置密码
    /*------------------------------------------------------ */
    public function restPassword()
    {
        $user_id = input("user_id/d");
        if ($user_id < 0) {
            return $this->error('获取用户ID传值失败！');
        }
        $data['password'] = f_hash('jx666666');
        $oldpassword = $this->Model->where('user_id', $user_id)->value('password');
        if ($data['password'] == $oldpassword) {
            return $this->error('当前用户密码为系统默认：jx666666，无须修改.');
        }
        $res = $this->Model->where('user_id', $user_id)->update($data);
        if ($res < 1) {
            return $this->error('未知错误，处理失败.');
        }
        $this->_log($user_id, '重置用户密码.', 'member');
        return $this->success('操作成功.');
    }
    /*------------------------------------------------------ */
    //-- 重置支付密码
    /*------------------------------------------------------ */
    public function restPayPassword()
    {
        $user_id = input("user_id/d");
        if ($user_id < 0) {
            return $this->error('获取用户ID传值失败！');
        }
        $pay_password = '999999';
        $data['pay_password'] = f_hash($pay_password);
        $res = $this->Model->where('user_id', $user_id)->update($data);
        if ($res < 1) {
            return $this->error('未知错误，处理失败.');
        }
        $this->_log($user_id, '重置用户支付密码.', 'member');
        return $this->success('操作成功,新支付密码：'.$pay_password,'',['alert'=>1]);
    }
    /*------------------------------------------------------ */
    //-- 会员管理
    /*------------------------------------------------------ */
    public function info()
    {
        $user_id = input('user_id/d');
        if ($user_id < 1) return $this->error('获取用户ID传值失败！');
        $row = $this->Model->info($user_id);
        if (empty($row)) return $this->error('用户不存在！');
        if($row['proxy']){
            $level_type = Db::name('user_region')->where('region',$row['proxy'])->value('level');
            $this->assign('level_type', $level_type);
            if($level_type == 1){
                $region = Db::name('bs_province')->where('PROVINCE_CODE',$row['proxy'])->value('PROVINCE_NAME');
            }elseif($level_type == 2){
                $region = Db::name('bs_city')->where('CITY_CODE',$row['proxy'])->value('CITY_NAME');
            }elseif($level_type  == 3){
                $region = Db::name('bs_area')->where('AREA_CODE',$row['proxy'])->value('AREA_NAME');
            }elseif($level_type == 4){
                $region = Db::name('bs_street')->where('STREET_CODE',$row['proxy'])->value('STREET_NAME');
            }else{
                $region = '无';
            }
            $this->assign('region', $region);
        }
        $row['wx'] = (new WeiXinUsersModel)->where('user_id', $user_id)->find();
        $this->assign("userShareStats", $this->Model->userShareStats($user_id));
        $row['user_address'] = Db::table('users_address')->where('user_id', $user_id)->select();
        $this->assign('row', $row);
        $this->assign('d_level', config('config.dividend_level'));
        $DividendRoleModel = new DividendRoleModel();
        $this->assign("roleList", $DividendRoleModel->getRows());
        $this->assign("teamCount", (new UsersBindModel)->where('pid', $user_id)->count());
        $where[] = ['dividend_uid', '=', $user_id];
        $where[] = ['status', 'in', [2, 3]];
        $DividendModel = new DividendModel();
        $dividend_amount = $DividendModel->where($where)->sum('dividend_amount');

        $this->assign("wait_money", $dividend_amount );
        $this->assign("start_date", date('Y/m/01', strtotime("-1 months")));
        $this->assign("end_date", date('Y/m/d'));

        return $this->fetch('sys_admin/users/info');
    }
    /*------------------------------------------------------ */
    //-- 修改分销身份
    /*------------------------------------------------------ */
    public function editRole()
    {
        $user_id = input('user_id', 0, 'intval');
        $row = $this->Model->info($user_id);
        $DividendRoleModel = new DividendRoleModel();
        $roleList = $DividendRoleModel->getRows();
        if ($this->request->isPost()) {
            $data['role_id'] = input('role_id', 0, 'intval');
            $this->checkUpData($row, $data);
            $data['last_up_role_time'] = time();
            $res = $this->Model->upInfo($user_id, $data);
            if ($res < 1) return $this->error('操作失败,请重试.');
            $info = '后台手工操作由【' . ($row['role_id'] == 0 ? '粉丝' : $roleList[$row['role_id']]['role_name']) . '】调整为【' .($data['role_id'] < 1 ? '粉丝' : $roleList[$data['role_id']]['role_name']) . '】';
            $this->_log($user_id, $info, 'member');
            return $this->success('修改分佣身份成功！', 'reload');
        }
        $this->assign("roleList", $roleList);
        $this->assign("row", $row);

        return response($this->fetch('sys_admin/users/edit_role'));
    }
    /*------------------------------------------------------ */
    //-- 封禁会员
    /*------------------------------------------------------ */
    public function evelBan()
    {
        $user_id = input('user_id', 0, 'intval');
        $row = $this->Model->info($user_id);
        if ($row['is_ban'] == 1) return $this->error('会员已被封禁，无须重复操作.');
        $data['is_ban'] = 1;
        $res = $this->Model->upInfo($user_id, $data);
        if ($res < 1) return $this->error();
        $this->_log($user_id, '后台封禁会员.', 'member');
        return $this->success('禁封成功.', 'reload');
    }
    /*------------------------------------------------------ */
    //-- 解禁会员
    /*------------------------------------------------------ */
    public function reBan()
    {
        $user_id = input('user_id', 0, 'intval');
        $row = $this->Model->info($user_id);
        if ($row['is_ban'] == 0) return $this->error('会员已被解禁，无须重复操作.');
        $data['is_ban'] = 0;
        $res = $this->Model->upInfo($user_id, $data);
        if ($res < 1) return $this->error('操作失败,请重试.');
        $this->_log($user_id, '后台解禁会员.', 'member');
        return $this->success('解禁成功.', 'reload');
    }
    /*------------------------------------------------------ */
    //-- 根据关键字查询
    /*------------------------------------------------------ */
    public function pubSearch()
    {
        $keyword = input('keyword', '', 'trim');

        if (!empty($keyword)) {
            $where = "( mobile LIKE '%" . $keyword . "%' OR user_id = '" . $keyword . "' OR nick_name LIKE '%" . $keyword . "%' OR mobile LIKE '%" . $keyword . "%')";
        }
        $_list = $this->Model->where($where)->field("user_id,mobile,nick_name,user_name")->limit(20)->select();
        foreach ($_list as $key => $row) {
            $_list[$key] = $row;
        }
        $result['list'] = $_list;
        $result['code'] = 1;
        return $this->ajaxReturn($result);
    }
    /*------------------------------------------------------ */
    //-- 下级名单
    /*------------------------------------------------------ */
    public function getChainList()
    {
        $user_id = input('user_id', 0, 'intval');
        if ($user_id < 1) {
            $result['list'] = [];
            return $this->ajaxReturn($result);
        }
        $DividendRole = (new DividendRoleModel)->getRows();
        $UsersBindModel = new UsersBindModel();
        $rows = $this->Model->field('user_id,nick_name,role_id')->where('pid', $user_id)->select();
        foreach ($rows as $key => $row) {
            $row['role_name'] = $DividendRole[$row['role_id']]['role_name'];
            $row['teamCount'] = $UsersBindModel->where('pid', $row['user_id'])->count() + 1;
            $rows[$key] = $row;
        }
        $result['list'] = $rows;
        return $this->ajaxReturn($result);
    }
    /*------------------------------------------------------ */
    //-- 上级名单
    /*------------------------------------------------------ */
    public function getSuperiorList()
    {
        $user_id = input('user_id', 0, 'intval');
        $result['code'] = 1;
        $result['list'] = $this->Model->getSuperiorList($user_id);
        return $this->ajaxReturn($result);
    }

    /*------------------------------------------------------ */
    //-- 执行统计
    /*------------------------------------------------------ */
    public function evalStat()
    {
        $reportrange = input('reportrange', '2019/01/01 - 2019/03/21', 'trim');
        $user_id = input('user_id', '29889', 'intval');

        $dtime = explode('-', $reportrange);
        $UsersBindModel = new UsersBindModel();
        $viewObj = $UsersBindModel->alias('b')->field('o.user_id,o.order_id,o.user_id,o.order_amount,o.dividend_amount,og.goods_name,og.goods_id,og.goods_name,og.goods_number,og.shop_price');
        $viewObj->join("shop_order_info o", 'b.user_id=o.user_id AND o.order_status = 1 AND o.add_time between ' . strtotime($dtime[0]) . ' and ' . (strtotime($dtime[1]) + 86399), 'left');
        $viewObj->join("shop_order_goods og", 'og.order_id=o.order_id', 'left');
         $rows = $viewObj->where('b.pid', $user_id)->select()->toArray();
        $result['buyGoods'] = [];
        $nowUser = [];
        $result['dividend_amount'] = $nowUser['dividend_amount'] = 0;
        $result['order_amount'] = $nowUser['order_amount'] = 0;
        $order_ids = $user_order_ids = [];
        $buy_ser_ids = [];
        foreach ($rows as $row) {
            if ($row['goods_id'] < 1)  continue;
            $order_ids[$row['order_id']] = 1;
            $buy_ser_ids[$row['user_id']] = 1;
            $result['buyGoods'][$row['goods_id']]['goods_name'] = $row['goods_name'];
            $result['buyGoods'][$row['goods_id']]['num'] += $row['goods_number'];
            $result['buyGoods'][$row['goods_id']]['price'] += $row['shop_price'];
            $result['dividend_amount'] += $row['dividend_amount'];
            $result['order_amount'] += $row['order_amount'];
        }
        $viewObj = $this->Model->alias('u')->field('o.user_id,o.order_id,o.user_id,o.order_amount,o.dividend_amount,og.goods_name,og.goods_id,og.goods_name,og.goods_number,og.shop_price');
        $viewObj->join("shop_order_info o", 'u.user_id=o.user_id AND o.order_status = 1 AND o.add_time between ' . strtotime($dtime[0]) . ' and ' . (strtotime($dtime[1]) + 86399), 'left');
        $viewObj->join("shop_order_goods og", 'og.order_id=o.order_id', 'left');
        $rows = $viewObj->where('u.user_id', $user_id)->select()->toArray();
        foreach ($rows as $row) {
            if ($row['goods_id'] < 1)  continue;
            $order_ids[$row['order_id']] = 1;
            $buy_ser_ids[$row['user_id']] = 1;
            $result['buyGoods'][$row['goods_id']]['goods_name'] = $row['goods_name'];
            $result['buyGoods'][$row['goods_id']]['num'] += $row['goods_number'];
            $result['buyGoods'][$row['goods_id']]['price'] += $row['shop_price'];
            $result['dividend_amount'] += $row['dividend_amount'];
            $result['order_amount'] += $row['order_amount'];

            $nowUser['buyGoods'][$row['goods_id']]['goods_name'] = $row['goods_name'];
            $nowUser['buyGoods'][$row['goods_id']]['num'] += $row['goods_number'];
            $nowUser['buyGoods'][$row['goods_id']]['price'] += $row['shop_price'];
            $user_order_ids[$row['order_id']] = 1;
            $nowUser['dividend_amount'] += $row['dividend_amount'];
            $nowUser['order_amount'] += $row['order_amount'];

        }

        $result['code'] = 1;
        $result['reportrange'] = $reportrange;
        $result['order_num'] = count($order_ids);
        $result['buy_user_num'] = count($buy_ser_ids);
        $nowUser['order_num'] = count($user_order_ids);
        $result['nowUser'] = $nowUser;
        return $this->ajaxReturn($result);
    }
     /*------------------------------------------------------ */
    //-- 修改所属上级
    /*------------------------------------------------------ */
    public function editSuperior()
    {

        $user_id = input('user_id', 0, 'intval');
        $userInfo = $this->Model->info($user_id);
        if ($this->request->isPost()) {
            $mkey = 'evaleditSuperior';
            $cache = Cache::get($mkey);
            if (empty($cache) == false){
                return $this->error('当前正在有人操作调整，请稍后再操作.');
            }
            Cache::get($mkey,true,60);
            $select_user_id = input('select_user', 0, 'intval');
            if ($select_user_id < 1){
                return $this->error('请选择需要修改的上级.');
            }
            if ($select_user_id == $userInfo['pid']){
                return $this->error('当前选择与当前会员上级一致，请核实.');
            }
            $where[] = ['pid','=',$user_id];
            $where[] = ['user_id','=',$select_user_id];
            $count = (new UsersBindModel)->where($where)->count();
            if ($count > 0){
                return $this->error('不能选择自己的下级作为上级.');
            }
            Db::startTrans();//启动事务

            $res = $this->Model->upInfo($user_id,['pid'=>$select_user_id]);

            if ($res < 1){
                Db::rollback();
                return $this->error('修改会员所属上级失败.');
            }

            $res = $this->Model->regUserBind($user_id,$select_user_id,true);//重新绑定当前用户的关系链
            if ($res == false){
                Db::rollback();
                return $this->error('绑定当前会员系链失败.');
            }
            $this->evaleditSuperior($user_id);//执行重新生成所有下属的关系链
            Db::commit();//事务，提交
            Cache::rm($mkey);
            $this->_log($user_id, '调整会员所属上级，原所属上级ID：'.$userInfo['pid'], 'member');
            return $this->success('修改所属上级成功！', 'reload');
        }
        if ($userInfo['pid'] > 0){
            $userInfo['puser'] = $this->Model->info($userInfo['pid']);
        }
        $this->assign("row", $userInfo);
        return response($this->fetch('sys_admin/users/edit_superior'));
    }

    /*------------------------------------------------------ */
    //-- 循执行调用更新关系链
    /*------------------------------------------------------ */
    protected function evaleditSuperior($pid,$level = 1){
        $bind_max_level = config('config.bind_max_level');
        if ($level > $bind_max_level){//循环到指定绑定层级跳出
            return true;
        }
        $users = $this->Model->where('pid',$pid)->field('user_id,pid')->select();
        if (empty($users)){
            return true;//没有找到下级不执行
        }
        foreach ($users as $user){
            $this->Model->regUserBind($user['user_id'],$pid,true);
            $this->evaleditSuperior($user['user_id'],$level + 1);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 导出
    /*------------------------------------------------------ */
    public function export(&$where)
    {
        $export_arr['会员ID'] = 'user_id';
        $export_arr['注册手机'] = 'mobile';
        $export_arr['昵称'] = 'user_name';
        $export_arr['推荐人ID'] = 'pid';
        $export_arr['等级'] = 'role_id';
        $export_arr['帐户余额'] = 'balance_money';

        $page = 0;
        $page_size = 500;
        $page_count = 100;
        $title = join("\t", array_keys($export_arr)) . "\t";

        $data = '';
        do {
            $rows = $this->Model->alias('u')->join("users_account uc", 'u.user_id=uc.user_id', 'left')->where(join(' AND ', $where))->limit($page * $page_size, $page_size)->select();
            if (empty($rows)) break;
            foreach ($rows as $row) {
                foreach ($export_arr as $val) {
                    if (strstr($val, '_time')) {
                        $data .= dateTpl($row[$val]) . "\t";
                    } elseif ($val == 'user_name') {
                        $data .= userInfo($row['user_id']). "\t";
                    } else {
                        $data .= str_replace(array("\r\n", "\n", "\r"), '', strip_tags($row[$val])) . "\t";
                    }
                }
                $data .= "\n";
            }
            $page++;
        } while ($page <= $page_count);

        $filename = '会员资料_' . date("YmdHis") . '.xls';
        $filename = iconv('utf-8', 'GBK//IGNORE', $filename);
        header("Content-type: application/vnd.ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename");
        echo iconv('utf-8', 'GBK//IGNORE', $title . "\n" . $data) . "\t";
        exit;
    }


    /**
     * 添加和修改用户区域代理商
     */
    public function RegionInfo()
    {
        $user_id = input('user_id/d');
        if ($user_id < 1) return $this->error('获取用户ID传值失败！');
        $row = $this->Model->info($user_id);
        if (empty($row)) return $this->error('用户不存在！');
        $level = 0;
        $data = [
            'type'=>0,
            'province_id'=>0,
            'province_name'=>'',
            'city_id'=>0,
            'city_name'=>'',
            'district_id'=>0,
            'district_name'=>'',
            'street_id'=>0,
            'street_name'=>'',
        ];
        $proxylevel = Db::table('user_region')->where('status',1)->where('user_id',$user_id)->where('region',$row['proxy'])->find();
        if($proxylevel){
            $level = $proxylevel['level'];
            $data['type'] = $proxylevel['level'];
        }
        switch ($level) {
            case 1:
                $ispro = Db::table('bs_province')->where('PROVINCE_CODE',$row['proxy'])->find();
                $data['province_id'] = $ispro['PROVINCE_CODE'];
                $data['province_name'] = $ispro['PROVINCE_NAME'];
            break;
            case 2:
                $ispro = Db::table('bs_city')->where('CITY_CODE',$row['proxy'])->find();
                $data['city_id'] = $ispro['CITY_CODE'];
                $data['city_name'] = $ispro['CITY_NAME'];
            break;
            case 3:
                $ispro = Db::table('bs_area')->where('AREA_CODE',$row['proxy'])->find();
                $data['district_id'] = $ispro['AREA_CODE'];
                $data['district_name'] = $ispro['AREA_NAME'];
            break;
            case 4:
                $ispro = Db::table('bs_street')->where('STREET_CODE',$row['proxy'])->find();
                $data['street_id'] = $ispro['STREET_CODE'];
                $data['street_name'] = $ispro['STREET_NAME'];
            break;
            default:
                
            break;
        }

        if($this->request->isPost()){
            $data = input('');
            $userinfo = $this->Model->info($data['user_id']);
            if(empty($userinfo)){
                return $this->error('操作失败！','index');
            }
            $data['name'] = $userinfo['nick_name'];
            $data['phone'] = $userinfo['mobile'];
            $data['region_address'] = '无';
            if(!$data['name'] || !$data['phone']  || !$data['region_address']  || !$data['type']  ){
                return $this->error('操作失败！','index');
            }
           
            switch ($data['type']) {
                case 1:
                    $Arr['region'] = $data['province_id'];
                    break;
                case 2:
                    $Arr['region'] = $data['city_id'];
                    break;
                case 3:
                    $Arr['region'] = $data['district_id'];
                    break;
                case 4:
                    $Arr['region'] = $data['street_id'];
                    break;
                default:
                    return $this->error('操作失败！','index');
                    break;
            }
            if(Db::name('user_region')->where(['level'=>$data['type'],'status'=>1,'region'=>$Arr['region']])->value('user_id')){
                return $this->error('已存在代理','index');
            }
            $Arr['user_id'] = $data['user_id'];
            $Arr['level']   = $data['type'];
            $Arr['order_id']= 0;
            $Arr['name']    = $data['name'];
            $Arr['phone']   = $data['phone'];
            $Arr['region_address'] = $data['region_address'];
            $Arr['status'] =  1;
                       
            $user_region = Db::name('user_region')->where(['user_id'=>$data['user_id'],'status'=>1])->find();
            if($user_region){
                $Arr['update_time'] = time();
                $re = Db::name('user_region')->where('user_id',$data['user_id'])->update($Arr);
                if(empty($re)) return $this->error('操作失败！','index');
            }else{
                $Arr['add_time'] = time();
                $re = Db::name('user_region')->insertGetId($Arr);
                if(empty($re)) return $this->error('操作失败！','index');
            }
            $res = Db::name('users')->where(['user_id'=>$user_id])->update(['proxy'=>$Arr['region']]);
            if($res !== false){
                return $this->success('操作成功！','index');
            }else{
                return $this->error('操作失败！','index');
            }
        }
        $this->assign('data', $data);
        $this->assign('user_id', $user_id);
        return response($this->fetch('sys_admin/users/region_info'));
    }

    /*------------------------------------------------------ */
    //-- 获取地区
    /*------------------------------------------------------ */
    public function getRegion(){
        $parent_id = input('parent_id');
        $selected = input('get.selected',0);
        $data = Db::name('main_region')->where("pid",$parent_id)->select();
        $html = '';
        if($data){
            foreach($data as $h){
                if($h['id'] == $selected){
                    $html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
                }
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        return $html;
    }

    /*------------------------------------------------------ */
    //-- 申请门店
    /*------------------------------------------------------ */
    public function distributionStore()
    {
        $user_id = input('user_id/d');

        if ($user_id < 1) return $this->error('获取用户ID传值失败！');
        $row = $this->Model->info($user_id);
        if (empty($row)) return $this->error('用户不存在！');
        if($row['proxy']){
            $level_type = Db::name('main_region')->where('id',$row['proxy'])->find();
            if($level_type['level_type'] == 1){
                $cityce= Db::name('main_region')->where('pid',$level_type['id'])->find();
                $district= Db::name('main_region')->where('pid',$cityce['id'])->find();
                $level_type['province_id'] = $level_type['id'];
                $level_type['city_id'] = $cityce['id'];
                $level_type['district_id'] = $district['id'];
            }elseif($level_type['level_type'] == 2){
                $province= Db::name('main_region')->where('id',$level_type['pid'])->find();
                $district= Db::name('main_region')->where('pid',$level_type['id'])->find();
                $level_type['province_id'] = $province['id'];
                $level_type['city_id'] = $level_type['id'];
                $level_type['district_id'] = $district['id'];
            }elseif($level_type['level_type'] == 3){
                $cityce= Db::name('main_region')->where('id',$level_type['pid'])->find();
                $province= Db::name('main_region')->where('id',$cityce['pid'])->find();
                $level_type['province_id'] = $province['id'];
                $level_type['city_id'] = $cityce['id'];
                $level_type['district_id'] = $level_type['id'];
            }
//            dump($level_type);exit;
            $this->assign('level_type', $level_type);
        }
        if($this->request->isPost()){

            $data = input('');
            if(!$data['type']  || !$data['province_id'] ){
                return $this->error('请填写完整.');
            }

            $user_region = Db::name('user_region')->where(['user_id'=>$user_id,'status'=>0])->find();
            if($user_region) return $this->error('您已有待审核信息.');
            if($data['type'] == 1){
                $proxy = Db::name('users')->where('proxy',$data['province_id'])->value('proxy');
                if($proxy) return $this->error('该代理已存在.');
                $region = $data['province_id'];
            }

            if($data['type'] == 2){
                if(!$data['city_id'])return $this->error('请选择城市.');
                $proxy = Db::name('users')->where('proxy',$data['city_id'])->value('proxy');
                if($proxy) return $this->error('该代理已存在.');
                $region = $data['city_id'];
            }

            if($data['type'] == 3){
                if(!$data['district_id'])return $this->error('请选择区域.');
                $proxy = Db::name('users')->where('proxy',$data['district_id'])->value('proxy');
                if($proxy) return $this->error('该代理已存在.');
                $region = $data['district_id'];
            }
            $res = Db::name('users')->where(['user_id'=>$user_id])->update(['proxy'=>$region]);
            if($res !== false){
                return $this->success('操作成功！','index');
            }else{
                return $this->error('操作失败！','index');
            }
        }

        $province = Db::name('main_region')->where('level_type',1)->field('id,name')->select();
        $city = Db::name('main_region')->where('level_type',2)->field('id,name')->select();
        $district = Db::name('main_region')->where('level_type',3)->field('id,name')->select();
        $this->assign('province', $province);
        $this->assign('city', $city);
        $this->assign('district', $district);
        $this->assign('user_id', $user_id);
        return response($this->fetch('sys_admin/users/region_info'));
    }

}

